﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore.Storage;
using S.Services.SheepMale.Api.Application.Command.MarketCommand;
using S.Services.SheepMale.Domain.PurchaseSales;
using S.Services.SheepMale.Infrastructure;
using System.Security.Claims;

namespace S.Services.SheepMale.Api.Application.CommandHandler.MarketCommandHandler
{
	public class MarketAddCommandHandler : IRequestHandler<MarketAddCommand, int>
	{

		private readonly IRepository<EntityMarket> entityrepository;

		private readonly IMapper mapper;
		public MarketAddCommandHandler(IRepository<EntityMarket> entityrepository, IMapper mapper)
		{
			this.entityrepository = entityrepository;
			this.mapper = mapper;
		}

		public async Task<int> Handle(MarketAddCommand request, CancellationToken cancellationToken)
		{

			var result = entityrepository.AllAsync().Result.Where(x => x.Earbugles == request.Earbugles && x.MarketId != request.MarketId);
			if (result.Count() != 0)
			{
				return -1;
			}

			request.CreateDate = DateTime.Now;
			request.AuditState = 1;
			request.SheepNumber=1;
			request.TotalPrice =request.SheepNumber*request.PayPrice;
			//var clientInfo = repository.GetAsync(request.MarketId);
			await entityrepository.CreateAsync(mapper.Map<EntityMarket>(request));
			return 1;
		}
	}
}
